package util.office;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;

import model.TStudent;
import persistent.factory.DAOFactory;
import util.toolkit.LogUtil;

public class Test {

	// public static String getExceptionAllInfo(Exception ex) {
	// ByteArrayOutputStream out = null;
	// PrintStream pout = null;
	// String ret = "";
	// try {
	// out = new ByteArrayOutputStream();
	// pout = new PrintStream(out);
	// ex.printStackTrace(pout);
	// ret = new String(out.toByteArray());
	// out.close();
	// }catch(Exception e){
	// return ex.getMessage();
	// }finally{
	// if(pout!=null){
	// pout.close();
	// }
	// }
	// return ret;
	// }

	/**
	 * 日期格式转换类，把yyyy/MM/dd类型的日期格式转换为yyyy-MM-dd的日期格式
	 * @param oldtime 需要转换的日期格式
	 * @return 返回一个转换后的日期格式
	 * @throws ParseException 会出现的异常信息
	 */
	public static String formatConversion(String oldtime) throws ParseException {
		SimpleDateFormat in = new SimpleDateFormat("yyyy/MM/dd");
		SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd");
		try {
			in.format(oldtime);
			return out.format(in.format(oldtime));
		} catch (Exception e) {
			//e.printStackTrace();
			return oldtime;			
		}
	}
	
	/**
	 * 获取异常详细信息，知道出了什么错，错在哪个类的第几行 .
	 * 
	 * @param e
	 * @return
	 */
	public static String getThrowableDetail(Throwable ex) {
		StringWriter sw = new StringWriter();
		try {
			PrintWriter pw = new PrintWriter(sw, true);
			ex.printStackTrace(pw);
			pw.flush();
			sw.flush();
		} catch (Exception e) {

		}
		return sw.toString();
	}

	/**
	 * 获取异常详细信息，知道出了什么错，错在哪个类的第几行 .
	 * 
	 * @param ex
	 * @return
	 */
	public static String getExceptionDetail(Error ex) {
		String ret = null;
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			PrintStream pout = new PrintStream(out);
			ex.printStackTrace(pout);
			ret = new String(out.toByteArray());
			pout.close();
			out.close();
		} catch (Exception e) {
		}
		return ret;
	}

	/**
	 * 获取异常详细信息，知道出了什么错，错在哪个类的第几行 .
	 * 
	 * @param ex
	 * @return
	 */
	public static String getExceptionDetail(Exception ex) {
		String ret = null;
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			PrintStream pout = new PrintStream(out);
			ex.printStackTrace(pout);
			ret = new String(out.toByteArray());
			pout.close();
			out.close();
		} catch (Exception e) {
		}
		return ret;
	}

	public static List removeDuplicate1(List list) {
		HashSet h = new HashSet(list);
		list.clear();
		list.addAll(h);
		return list;
	}

	public static List removeDuplicate2(List list) {
		for (int i = 0; i < list.size() - 1; i++) {
			for (int j = list.size() - 1; j > i; j--) {
				if (list.get(j).equals(list.get(i))) {
					list.remove(j);
				}
			}
		}
		return list;
	}

	public static List<TStudent> single(List<TStudent> list) {
		HashSet<TStudent> hash = new HashSet<TStudent>();
		hash.addAll(list);
		list.clear();
		list.addAll(hash);
		return list;
	}

	public static List<TStudent> getSingle(List<TStudent> list) {
		List<TStudent> newArrayList = new ArrayList<TStudent>();
		
		ListIterator<TStudent> iter = list.listIterator();
		while (iter.hasNext()) {
			TStudent per = (TStudent) iter.next();
			if (!newArrayList.contains(per)) {// contains方法判断是否包含,底层依赖equals方法
				newArrayList.add(per); // remove方法判断是否删除,底层依赖的也是equals方法
			}
		}

		return newArrayList;

	}
	
	// 删除ArrayList中重复元素
    public static List<TStudent> removeDuplicate(List<TStudent> list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).getStuid().equals(list.get(i).getStuid())) {
                    list.remove(j);
                }
            }
        }
        return list;
    }

	public static void main(String[] args) {

		List<TStudent> students = new ArrayList<TStudent>();
		TStudent student = new TStudent();
		student.setBirthday("2018-01-01");
		student.setClassid("0001");
		student.setPwd("");
		student.setRealName("徐震霖");
		student.setSchoolid("yngsxy");
		student.setSex("nan");
		student.setStuid("15612200036");
		students.add(student);

		TStudent student1 = new TStudent();
		student1.setBirthday("2018-01-01");
		student1.setClassid("0001");
		student1.setPwd("");
		student1.setRealName("黄璇");
		student1.setSchoolid("yngsxy");
		student1.setSex("nv");
		student1.setStuid("15612200037");
		students.add(student1);

		TStudent student2 = new TStudent();
		student2.setBirthday("2018-01-01");
		student2.setClassid("0001");
		student2.setPwd("");
		student2.setRealName("黄璇");
		student2.setSchoolid("yngsxy");
		student2.setSex("nv");
		student2.setStuid("15612200037");
		students.add(student2);

		List<TStudent> students2 = removeDuplicate(students);
		for (TStudent tStudent : students2) {
			System.out.println(tStudent.getRealName());
		}

		// 失败第1种
		// try {
		// TStudent student = new TStudent();
		// student.setBirthday(vReportImport.getBirthday());
		// student.setClassid(vReportImport.getClassId());
		// student.setRealName(vReportImport.getRealName());
		// student.setSchoolid(vReportImport.getSchoolid());
		// student.setSex(vReportImport.getSex());
		// student.setStuid(vReportImport.getUsername());
		// //保存学生信息
		// session1.save(student);
		// //再保存报告信息
		//
		// } catch (Exception e) {
		// String eMsg = e.getCause().getMessage();
		// if(eMsg.contains("UNIQUE KEY") && eMsg.contains("IX_T_Student") &&
		// eMsg.contains("dbo.T_Student")
		// && eMsg.contains("插入重复键") && eMsg.contains("重复键值为")){
		// Serializable key = session1.save(checkReport);
		// row = ((Integer)key).intValue();
		// }
		// else {
		// issuc = false;
		// break;
		// }
		// }
		// 违反了 UNIQUE KEY 约束“IX_T_Student”。不能在对象“dbo.T_Student”中插入重复键。重复键值为
		// (530128200508073617)

		// 失败第2种
		// try {
		// //先保存报告
		// Serializable key = session1.save(checkReport);
		// row = ((Integer)key).intValue();
		// } catch (Exception e) {
		// String eMsg = e.getCause().getMessage();
		// if(eMsg.contains("INSERT") && eMsg.contains("FOREIGN KEY") &&
		// eMsg.contains("FK_T_CheckReport_T_Student")
		// && eMsg.contains("dbo.T_Student") && eMsg.contains("stuid")){
		// TStudent student = new TStudent();
		// student.setBirthday(vReportImport.getBirthday());
		// student.setClassid(vReportImport.getClassId());
		// student.setRealName(vReportImport.getRealName());
		// student.setSchoolid(vReportImport.getSchoolid());
		// student.setSex(vReportImport.getSex());
		// student.setStuid(vReportImport.getUsername());
		// //保存学生信息
		// session1.save(student);
		// //再保存报告信息
		// Serializable key = session1.save(checkReport);
		// row = ((Integer)key).intValue();
		// }
		// else {
		// if(tx1 != null) tx1.rollback();
		// session1.close(); //关闭连接
		// log.error("persistent.impl.DataImportImpl类importEPQandMHTList(List<VReportImport> rImports)方法调用错误：");
		// log.error(LogUtil.getMsg(e));
		// System.out.println("persistent.impl.DataImportImpl类importEPQandMHTList(List<VReportImport> rImports)方法调用错误：");
		// e.printStackTrace();
		// issuc = false;
		// }
		// }

		// try {
		// List<String> test = new ArrayList<String>();
		// test.add("徐震霖");
		// System.out.println(test.get(0));
		// test.clear();
		// System.out.println(test.get(0));
		// } catch (Exception e) {
		// System.out.println(e.getStackTrace());// 这么写是不能打印出详细错误信息的
		// System.out.println(e.getMessage());// 这么写只能知道出了什么错，也不能打印出详细错误信息的
		// System.out.println(getExceptionDetail(e));//这个可以
		// System.out.println(getThrowableDetail(e));//这个可以
		// }
		// catch (Error e) {
		// System.out.println(getExceptionDetail(e));
		// }

		// boolean row =
		// DAOFactory.getStudentDAO().isexist("530326200410194611");
		// System.out.println(row);

	}

}
